package com.sunda.spmscommon.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sunda.spmscommon.entity.SysLog;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.ClobTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统日志表 Mapper 接口
 * </p>
 *
 * @author Wayne
 * @since 2020-12-16
 */
@Component
@Mapper
public interface SysLogMapper extends BaseMapper<SysLog> {

    @Select("<script>SELECT s.UUID AS \"uuid\",\n" +
            "  s.LOG_TYPE AS \"logType\",\n" +
            "  s.LOG_CONTENT AS \"logContent\",\n" +
            "  s.OPERATE_TYPE AS \"operateType\",\n" +
            "  s.USER_ID AS \"userId\",\n" +
            "  u.LAST_NAME AS \"userName\",\n" +
            "  s.IP AS \"ip\",\n" +
            "  s.METHOD AS \"method\",\n" +
            "  s.REQUEST_URL AS \"requestUrl\",\n" +
            "  s.REQUEST_PARAM AS \"requestParam\",\n" +
            "  s.REQUEST_TYPE AS \"requestType\",\n" +
            "  s.COST_TIME AS \"costTime\",\n" +
            "  s.CREATE_TIME AS \"createTime\",\n" +
            "  s.LOG_AUTO_ID AS \"logAutoId\",\n" +
            "  s.REMARKS AS \"remarks\"\n" +
            "FROM SYS_LOG s\n" +
            "  LEFT JOIN SPMS_USER u ON s.USER_ID = u.WORK_CODE\n" +
            "WHERE 1=1" +
            "<when test='logType!=null'> AND s.LOG_TYPE = #{logType}</when> " +
            "ORDER BY s.LOG_AUTO_ID DESC" +
            "</script>")
    @Results({
            @Result(column = "requestParam", property = "requestParam", javaType = String.class,
                    jdbcType = JdbcType.CLOB, typeHandler = ClobTypeHandler.class),
            @Result(column = "remarks", property = "remarks", javaType = String.class,
                    jdbcType = JdbcType.CLOB, typeHandler = ClobTypeHandler.class)
    })
    List<Map<String, Object>> getAllSysLog(@Param("logType") String logType);

    @Select("<script>SELECT s.UUID AS \"uuid\",\n" +
            "  s.LOG_TYPE AS \"logType\",\n" +
            "  s.LOG_CONTENT AS \"logContent\",\n" +
            "  s.OPERATE_TYPE AS \"operateType\",\n" +
            "  s.USER_ID AS \"userId\",\n" +
            "  u.LAST_NAME AS \"userName\",\n" +
            "  s.IP AS \"ip\",\n" +
            "  s.METHOD AS \"method\",\n" +
            "  s.REQUEST_URL AS \"requestUrl\",\n" +
            "  s.REQUEST_PARAM AS \"requestParam\",\n" +
            "  s.REQUEST_TYPE AS \"requestType\",\n" +
            "  s.COST_TIME AS \"costTime\",\n" +
            "  s.CREATE_TIME AS \"createTime\",\n" +
            "  s.LOG_AUTO_ID AS \"logAutoId\",\n" +
            "  s.REMARKS AS \"remarks\"\n" +
            "FROM SYS_LOG s\n" +
            "  LEFT JOIN SPMS_USER u ON s.USER_ID = u.WORK_CODE\n" +
            "WHERE 1=1" +
            "<when test='uuid!=null'> AND s.UUID = #{uuid}</when> " +
            "ORDER BY s.LOG_AUTO_ID DESC" +
            "</script>")
    @Results({
            @Result(column = "requestParam", property = "requestParam", javaType = String.class,
                    jdbcType = JdbcType.CLOB, typeHandler = ClobTypeHandler.class),
            @Result(column = "remarks", property = "remarks", javaType = String.class,
                    jdbcType = JdbcType.CLOB, typeHandler = ClobTypeHandler.class)
    })
    Map<String, Object> getByUuid(@Param("uuid") String uuid);

}
